#!/bin/bash

if [ "$1" != "" ]; then
   target="$1"
else
   target="./xrnlib-cli-autocomplete.sh"
fi

# get the checker file
pathtolexer=$(cat filelist.json | grep "lexcheck.h" | tr -d '"' | tr -d ',' | sed 's/  *//' | sed 's/^\./\.\.\/\.\./')

# get the list of options that need to be followed by a writing path
str_to_eval=$(cat $pathtolexer | grep XARG_PATH_W | sed '1,2d' | awk '{print $2}' | sed 's/^[^_]*[_]//' | sed 's/_/-/g' | tr '[:upper:]' '[:lower:]' | sort | uniq | sed 's/^/\"-/' | sed 's/$/\"/' | tr -s '\n' ' ' | sed 's/^/options_followed_by_w_path=(/' | sed 's/$/)/' )
eval "$str_to_eval"

# get the list of options that need to be followed by a writing path
str_to_eval=$(cat $pathtolexer | grep XARG_PATH_R | sed '1,2d' | awk '{print $2}' | sed 's/^[^_]*[_]//' | sed 's/_/-/g' | tr '[:upper:]' '[:lower:]' | sort | uniq | sed 's/^/\"-/' | sed 's/$/\"/' | tr -s '\n' ' ' | sed 's/^/options_followed_by_r_path=(/' | sed 's/$/)/' )
eval "$str_to_eval"

# get the list of all possible options and arguments
str_to_eval="$(cat $pathtolexer | grep -v XSTR_ALL_ARGS_UNIQ | grep XSTR_ALL_ARGS | sed 's/.*{/{/g' | tail -n 1 | tr -s '{' '(' | tr -s '}' ')' | tr -d ';' | tr -d ','  | sed 's/^/all_possible_args=/')"
eval "$str_to_eval"
#echo "${all_possible_args[@]}"

# create command and strip_command lists
list_strip_commands=()
list_of_commands=()
for word in ${all_possible_args[@]}; do
    # check if it is a command
    if  [[ $word == --* ]]; then
       list_of_commands+=("$word")
       if [ "$word" != "--help" ] && [ "$word" != "--example" ] && [ "$word" != "--script" ] && [ "$word" != "--version" ] ; then
           if [ "$word" != "--decoding-conf" ] && [ "$word" != "--encoding-conf" ] && [ "$word" != "--permutation-conf" ] && [ "$word" != "--sequence-conf" ] ; then
               if [ "$word" != "--arithmetic-conf" ] && [ "$word" != "--logging-conf" ] ; then
                   list_strip_commands+=("${word#??}")
               fi
           fi
       fi
    fi
done

all_possible_args_str=$(echo "${all_possible_args[@]}" | sed 's/ /" "/g' | sed 's/^/all_possible_args=("/' | sed 's/$/")/' )
#echo $all_possible_args_str

tree_of_commands_str=$(echo "${list_strip_commands[@]}" | tr -s ' ' '\n' | xargs -I _ sh -c "xrnlib-cli --help _ | tail -n +3 | sed -E 's/^(.{1,50}).*$/\1/' |  sed '/^a valid example/q' | head -n -1 | grep -v ' -' | tr -s '\n' ' ' | sed 's/$/\n/' | sed 's/^ //' " | tr -s '\n' '+' | sed 's/+$//' | sed 's/^/--logging-conf --help +--logging-conf --example +--logging-conf --script +/' | sed 's/^/tree_of_commands="/' | sed 's/$/"/' )
#echo $tree_of_commands_str

eval "$tree_of_commands_str"
possible_first_commands_str=$( echo "$tree_of_commands" | tr -s '+' '\n' | sed 's/--logging-conf//' | sed 's/--get-capacity//' | awk '{ print $1 }' | sed 's/^/--logging-conf /' | tr -s ' ' '\n' | sort | uniq | tr '\n' '+' | sed 's/+/" "/g' | sed 's/^/--example --scripts --help --version /' | sed 's/^/possible_first_commands=( "/' | sed 's/"$/)/' )
#echo $possible_first_commands_str

options_followed_by_w_path_str=$( echo ${options_followed_by_w_path[@]} | tr -s ' ' '\n' | sed 's/^/\"/' | sed 's/$/\"/' | tr -s '\n' ' ' | sed 's/$/)/' | sed 's/^/options_followed_by_w_path=(/' )
#echo $options_followed_by_w_path_str

options_followed_by_r_path_str=$( echo ${options_followed_by_r_path[@]} | tr -s ' ' '\n' | sed 's/^/\"/' | sed 's/$/\"/' | tr -s '\n' ' ' | sed 's/$/)/' | sed 's/^/options_followed_by_r_path=(/' )
#echo $options_followed_by_r_path_str

cat ../../archives/resources/miscellaneus/xrnlib-cli-autocomplete-template.txt | sed "s/#autocompleteall_possible_args/$all_possible_args_str/" \
                                                                               | sed "s/#autocompletetree_of_commands/$tree_of_commands_str/" \
                                                                               | sed "s/#autocompleteoptions_followed_by_w_path/$options_followed_by_w_path_str/" \
                                                                               | sed "s/#autocompleteoptions_followed_by_r_path/$options_followed_by_r_path_str/" \
                                                                               | sed "s/#autocompletepossible_first_commands/$possible_first_commands_str/" > \
    $target

